package sistema.controleVendas.persistencia;

import java.util.HashMap;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Conexao {
	static HashMap<String, String> map;
	static EntityManagerFactory emf;
	static EntityManager em;
	
	public static EntityManager contectar(){
		try{
			if(em!=null){
				return em;
			}else{
				map = new HashMap<String,String>();
				map.put(Persistence.PERSISTENCE_PROVIDER, "org.apache.openjpa.persistence.PersistenceProviderImpl");
				map.put("openjpa.ConnectionDriverName", "org.postgresql.Driver");
				map.put("openjpa.ConnectionURL", "jdbc:postgresql://localhost:5432/controledevendas");
				map.put("openjpa.ConnectionUserName", "postgres");
				map.put("openjpa.ConnectionPassword", "postgres");
				map.put("openjpa.jdbc.SynchronizeMappings","buildSchema(SchemaAction=refresh)");
				emf = Persistence.createEntityManagerFactory("ControleVendas", map);
				em = emf.createEntityManager();
			}
		}catch (Exception e) {
			e.getMessage();
		}
		return em;
	}
	
	public static void desconectar(){
		try{
			if(em!=null){
				em.close();
				emf.close();
			}
		}catch (Exception e) {
			e.getMessage();
		}

	}
}

